package com.xiaoyu;


import com.xiaoyu.client.Class_Teacher_CourseDao;
import com.xiaoyu.model.Account;
import com.xiaoyu.model.Teacher;
import com.xiaoyu.service.AccountService;
import com.xiaoyu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = "教师信息接口")
@RestController
//@CrossOrigin(origins = "http://39.101.65.116:8080")
@CrossOrigin(origins = "http://localhost:8080")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private Class_Teacher_CourseDao classTeacherCourseDao;
    @Autowired
    private AccountService accountService;

    @ApiOperation("教师信息查询接口")
    @GetMapping("/teacherInfo")
    public Teacher teacherInfo(HttpSession session){
        Account a= (Account) session.getAttribute("loginAccount");
        if (a!=null){
            Teacher teacher =new Teacher();
            teacher.setAid(a.getId());
            Teacher t=teacherService.findOneTeacher(teacher);
            session.setAttribute("teacher",t);
            return t;
        }
        return null;
    }

    @ApiOperation("教师列表接口")
    @GetMapping("/teacherList")
    public List<Teacher> teacherList(){
        return teacherService.allTeacher();
    }

    @ApiOperation("教师添加接口")
    @PutMapping("/teacher/{post}/{nameFirstCode}/{courseId}/{cid}")
    public String addTeacher(@PathVariable String post,
                             @PathVariable String nameFirstCode,
                             @PathVariable Integer courseId,
                             @PathVariable Integer cid,
                             @RequestBody Teacher teacher
                             ){
        if(classTeacherCourseDao.select(cid,null,courseId)==1){
            return "own";
        }
        String accountName;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy");
        //设置入职时年龄
        Integer inductionTime= Integer.parseInt(sdf.format(teacher.getInductionTime()));
        Integer tbirthday= Integer.parseInt(sdf.format(teacher.getTbirthday()));
        teacher.setTage(inductionTime-tbirthday);
        //账户信息
        Account account=new Account();
        accountName=nameFirstCode+
                sdf.format(teacher.getInductionTime())+
                teacher.getTage()+System.currentTimeMillis()%10000;
        account.setAccountName(accountName);
        //添加账户
        accountService.addAccount(account);
        //添加教师
        Boolean b=teacherService.addTeacher(teacher,post,account);
        //添加教师与班级课程关系
        teacherService.addTeacherClient(teacher,courseId,cid);

        if (b)
            return "success";
        return "error";
    }

    @ApiOperation("删除教师接口")
    @DeleteMapping("/teacher")
    public String deleteTeacher(@RequestBody Teacher teacher){
        teacherService.deleteTeacherClient(teacher);
        Account a=new Account();
        a.setId(teacher.getAid());
        accountService.deleteAccount(a);
        if(teacherService.deleteTeacher(teacher))
            return "success";
        return "error";
    }

    @ApiOperation("一键罢职接口")
    @PostMapping("/quitJob")
    public String quitJob(@RequestBody String quitList){
        int i= quitList.indexOf('[');
        int j= quitList.indexOf(']');
        //字符串转int类型List,l:字符串类型List,qList:int类型List
        List<String> l=Arrays.asList(quitList.substring(i+1,j).split(","));
        Teacher t=new Teacher();
        t.setIsQuit(1);
        t.setQuitTime(new Date());
        Account a=new Account();
        a.setState(0);
        for (String quit:l) {
            t.setId(Integer.parseInt(quit));
            teacherService.deleteTeacherClient(t);
            a.setId(teacherService.findTeacher(t).getAid());
            accountService.updateAccount(a);
            teacherService.updateTeacher(t);
        }

        return "success";
    }

    @ApiOperation("教师修改接口")
    @PostMapping("/teacher")
    public String updateTeacher(@RequestBody Teacher teacher){
        if(teacherService.updateTeacher(teacher))
            return "success";
        return "error";
    }
}
